// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.characters;

import java.util.Iterator;
import java.util.LinkedHashMap;
import zombie.core.Core;
import zombie.core.logger.LoggerManager;
import zombie.core.math.PZMath;
import zombie.debug.DebugLog;
import zombie.debug.LogSeverity;
import zombie.iso.areas.NonPvpZone;
import zombie.network.GameServer;
import zombie.network.ServerOptions;
import zombie.util.Type;

public class SafetySystemManager {
    private static final LinkedHashMap<String, Float> playerCooldown = new LinkedHashMap();
    private static final LinkedHashMap<String, Boolean> playerSafety = new LinkedHashMap();
    private static final LinkedHashMap<String, Long> playerDelay = new LinkedHashMap();
    private static final long safetyDelay = 1500L;

    private static void updateTimers(Safety safety) {
        float _float = zombie.GameTime.instance.getRealworldSecondsSinceLastUpdate();
        if (safety.getToggle() > 0.0F) {
            safety.setToggle(safety.getToggle() - _float);
            if (safety.getToggle() <= 0.0F) {
                safety.setToggle(0.0F);
                if (!safety.isLast()) {
                    safety.setEnabled(!safety.isEnabled());
                }
            }
        } else if (safety.getCooldown() > 0.0F) {
            safety.setCooldown(safety.getCooldown() - _float);
        } else {
            safety.setCooldown(0.0F);
        }
    }

    private static void updateNonPvpZone(IsoPlayer isoPlayer, boolean _boolean) {
        if (_boolean && !isoPlayer.networkAI.wasNonPvpZone) {
            storeSafety(isoPlayer);
            GameServer.sendChangeSafety(isoPlayer.getSafety());
        } else if (!_boolean && isoPlayer.networkAI.wasNonPvpZone) {
            restoreSafety(isoPlayer);
            GameServer.sendChangeSafety(isoPlayer.getSafety());
        }

        isoPlayer.networkAI.wasNonPvpZone = _boolean;
    }

    static void update(IsoPlayer isoPlayer) {
        boolean _boolean = NonPvpZone.getNonPvpZone(PZMath.fastfloor(isoPlayer.getX()), PZMath.fastfloor(isoPlayer.getY())) != null;
        if (!_boolean) {
            updateTimers(isoPlayer.getSafety());
        }

        if (GameServer.bServer) {
            updateNonPvpZone(isoPlayer, _boolean);
        }
    }

    public static void clear() {
        playerCooldown.clear();
        playerSafety.clear();
        playerDelay.clear();
    }

    public static void clearSafety(IsoPlayer player) {
        if (player != null) {
            Safety safety = player.getSafety();
            LoggerManager.getLogger("pvp").write(String.format("user \"%s\" clear safety %s", player.getUsername(), safety.getDescription()), "INFO");
            playerCooldown.remove(player.getUsername());
            playerSafety.remove(player.getUsername());
            playerDelay.remove(player.getUsername());
        } else if (Core.bDebug) {
            DebugLog.Combat.debugln("ClearSafety: player not found");
        }
    }

    public static void storeSafety(IsoPlayer player) {
        try {
            if (player != null && player.isAlive()) {
                Safety safety = player.getSafety();
                LoggerManager.getLogger("pvp").write(String.format("user \"%s\" store safety %s", player.getUsername(), safety.getDescription()), "INFO");
                playerSafety.put(player.getUsername(), safety.isEnabled());
                playerCooldown.put(player.getUsername(), safety.getCooldown());
                playerDelay.put(player.getUsername(), System.currentTimeMillis());
                if (playerCooldown.size() > ServerOptions.instance.MaxPlayers.getValue() * 1000) {
                    Iterator iterator0 = playerCooldown.entrySet().iterator();
                    if (iterator0.hasNext()) {
                        iterator0.next();
                        iterator0.remove();
                    }
                }

                if (playerSafety.size() > ServerOptions.instance.MaxPlayers.getValue() * 1000) {
                    Iterator iterator1 = playerSafety.entrySet().iterator();
                    if (iterator1.hasNext()) {
                        iterator1.next();
                        iterator1.remove();
                    }
                }

                if (playerDelay.size() > ServerOptions.instance.MaxPlayers.getValue() * 1000) {
                    Iterator iterator2 = playerDelay.entrySet().iterator();
                    if (iterator2.hasNext()) {
                        iterator2.next();
                        iterator2.remove();
                    }
                }
            } else if (Core.bDebug) {
                DebugLog.Combat.debugln("StoreSafety: player not found");
            }
        } catch (Exception exception) {
            DebugLog.Multiplayer.printException(exception, "StoreSafety failed", LogSeverity.Error);
        }
    }

    public static void restoreSafety(IsoPlayer player) {
        try {
            if (player != null) {
                Safety safety = player.getSafety();
                if (playerSafety.containsKey(player.getUsername())) {
                    safety.setEnabled((Boolean)playerSafety.remove(player.getUsername()));
                }

                if (playerCooldown.containsKey(player.getUsername())) {
                    safety.setCooldown((Float)playerCooldown.remove(player.getUsername()));
                }

                playerDelay.put(player.getUsername(), System.currentTimeMillis());
                LoggerManager.getLogger("pvp").write(String.format("user \"%s\" restore safety %s", player.getUsername(), safety.getDescription()), "INFO");
            } else if (Core.bDebug) {
                DebugLog.Combat.debugln("RestoreSafety: player not found");
            }
        } catch (Exception exception) {
            DebugLog.Multiplayer.printException(exception, "RestoreSafety failed", LogSeverity.Error);
        }
    }

    public static void updateOptions() {
        boolean boolean0 = ServerOptions.instance.PVP.getValue();
        boolean boolean1 = ServerOptions.instance.SafetySystem.getValue();
        if (!boolean0) {
            clear();

            for (IsoPlayer isoPlayer0 : GameServer.IDToPlayerMap.values()) {
                if (isoPlayer0 != null) {
                    isoPlayer0.getSafety().setEnabled(true);
                    isoPlayer0.getSafety().setLast(false);
                    isoPlayer0.getSafety().setCooldown(0.0F);
                    isoPlayer0.getSafety().setToggle(0.0F);
                    GameServer.sendChangeSafety(isoPlayer0.getSafety());
                }
            }
        } else if (!boolean1) {
            clear();

            for (IsoPlayer isoPlayer1 : GameServer.IDToPlayerMap.values()) {
                if (isoPlayer1 != null) {
                    isoPlayer1.getSafety().setEnabled(false);
                    isoPlayer1.getSafety().setLast(false);
                    isoPlayer1.getSafety().setCooldown(0.0F);
                    isoPlayer1.getSafety().setToggle(0.0F);
                    GameServer.sendChangeSafety(isoPlayer1.getSafety());
                }
            }
        }
    }

    public static boolean checkUpdateDelay(IsoGameCharacter wielder, IsoGameCharacter target) {
        boolean boolean0 = false;
        IsoPlayer isoPlayer0 = Type.tryCastTo(wielder, IsoPlayer.class);
        IsoPlayer isoPlayer1 = Type.tryCastTo(target, IsoPlayer.class);
        if (isoPlayer0 != null && isoPlayer1 != null) {
            long long0 = System.currentTimeMillis();
            if (playerDelay.containsKey(isoPlayer0.getUsername())) {
                long long1 = long0 - (Long)playerDelay.getOrDefault(isoPlayer0.getUsername(), 0L);
                boolean boolean1 = long1 < 1500L;
                boolean0 = boolean1;
                if (!boolean1) {
                    playerDelay.remove(isoPlayer0.getUsername());
                }
            }

            if (playerDelay.containsKey(isoPlayer1.getUsername())) {
                long long2 = long0 - (Long)playerDelay.getOrDefault(isoPlayer1.getUsername(), 0L);
                boolean boolean2 = long2 < 1500L;
                if (!boolean0) {
                    boolean0 = boolean2;
                }

                if (!boolean2) {
                    playerDelay.remove(isoPlayer1.getUsername());
                }
            }
        }

        return boolean0;
    }
}
